unit untMain_PD280;

interface

uses
  Windows, Messages, SysUtils, Forms, Dialogs, StdCtrls, ActnList, ExtDlgs, jpeg,
  Vcl.CheckLst, Vcl.ExtCtrls, Vcl.ComCtrls, untConstant, Vcl.ButtonGroup,
  Vcl.Controls, System.Classes;

type
  TOpenComm = function(sComm: AnsiString; iBaudRate: Integer): Boolean; stdcall;

  TCloseComm = procedure; stdcall;

  TLED_Display = function(sText: AnsiString; LightStatus: Integer): Integer; stdcall;

  TDSP_Init = function(): Integer; stdcall;

  TDSP_Restart = function(): Integer; stdcall;

  TDSP_GetFirmwareVersion = function(): Integer; stdcall;

  TDSP_UploadPicture = function(FileName: AnsiString; FilePathName: AnsiString): Integer; stdcall;

  TDSP_SetDefaultStyle = function(n: Integer): Integer; stdcall;

  TDSP_SwitchStyle = function(n: Integer): Integer; stdcall;

  TDSP_ShowPicture_3 = function(FileName: AnsiString; X, Y: Integer): Integer; stdcall;

  TDSP_ShowQrCode_3 = function(sText: AnsiString; BarcodeSize, X, Y: Integer): Integer; stdcall;

  TDSP_ShowText_3 = function(sText: AnsiString; FontSize, X, Y: Integer): Integer; stdcall;

  TSetDebugFlag = procedure(debug: Boolean); stdcall;

  TfrmMain_PD280 = class(TForm)
    btn_exit: TButton;
    grp_serial: TGroupBox;
    Label1: TLabel;
    btn_open: TButton;
    cbbPort: TComboBox;
    btn_close: TButton;
    btn_Refresh: TButton;
    OpenPic1: TOpenPictureDialog;
    btn_getfirmwareversion: TButton;
    btn_restart: TButton;
    GroupBox1: TGroupBox;
    lbl_file_style3: TLabel;
    cbb_fileno_style3: TComboBox;
    Label2: TLabel;
    edt_x_picture_style3: TEdit;
    Label4: TLabel;
    edt_y_picture_style3: TEdit;
    btn_showpicture_style3: TButton;
    btn_uploadpicture_style3: TButton;
    btn_selectfile_style3: TButton;
    edt_filename_style3: TEdit;
    Label13: TLabel;
    Label6: TLabel;
    mmo_barcodetext_style3: TMemo;
    edt_barcodesize: TEdit;
    Label8: TLabel;
    edt_x_barcode_style3: TEdit;
    Label9: TLabel;
    edt_y_barcode_style3: TEdit;
    btn_showbarcode_style3: TButton;
    edt_y_text_style3: TEdit;
    Label10: TLabel;
    edt_x_text_style3: TEdit;
    Label11: TLabel;
    edt_fontsize_style3: TEdit;
    Label12: TLabel;
    Label7: TLabel;
    mmo_messagetext_style3: TMemo;
    rg_lightstatus: TRadioGroup;
    lbl_amount: TLabel;
    edt_amount: TEdit;
    btn_display_led8: TButton;
    btn_init: TButton;
    btn_showtext_style3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn_openClick(Sender: TObject);
    procedure btn_closeClick(Sender: TObject);
    procedure btn_exitClick(Sender: TObject);
    procedure btn_RefreshClick(Sender: TObject);
    procedure btn_getfirmwareversionClick(Sender: TObject);
    procedure btn_restartClick(Sender: TObject);
    procedure edt_amountKeyPress(Sender: TObject; var Key: Char);
    procedure btn_display_led8Click(Sender: TObject);
    procedure btn_selectfile_style3Click(Sender: TObject);
    procedure btn_uploadpicture_style3Click(Sender: TObject);
    procedure btn_showpicture_style3Click(Sender: TObject);
    procedure btn_showbarcode_style3Click(Sender: TObject);
    procedure btn_showtext_style3Click(Sender: TObject);
    procedure btn_initClick(Sender: TObject);
  private
    { Private declarations }
    IsOpened: Boolean;
    procedure ToggleEnabled;
  public
    { Public declarations }
  end;

var
  frmMain_PD280: TfrmMain_PD280;
  dllhandle: THandle;
  OpenComm: TOpenComm;
  CloseComm: TCloseComm;
  LED_Display: TLED_Display;
  DSP_Init:TDSP_Init;
  DSP_Restart: TDSP_Restart;
  DSP_GetFirmwareVersion: TDSP_GetFirmwareVersion;
  DSP_UploadPicture: TDSP_UploadPicture;
  DSP_SetDefaultStyle: TDSP_SetDefaultStyle;
  DSP_SwitchStyle: TDSP_SwitchStyle;
  DSP_ShowPicture_3: TDSP_ShowPicture_3;
  DSP_ShowQrCode_3: TDSP_ShowQrCode_3;
  DSP_ShowText_3: TDSP_ShowText_3;
  SetDebugFlag: TSetDebugFlag;

implementation

{$R *.dfm}

procedure TfrmMain_PD280.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  dllhandle := SafeLoadLibrary('TPD280DLL.dll');
  if (dllhandle <> 0) then
  begin
    @OpenComm := GetProcAddress(dllhandle, 'OpenComm');
    @CloseComm := GetProcAddress(dllhandle, 'CloseComm');

    @LED_Display := GetProcAddress(dllhandle, 'LED_Display');

    @DSP_Init := GetProcAddress(dllhandle, 'DSP_Init');
    @DSP_Restart := GetProcAddress(dllhandle, 'DSP_Restart');
    @DSP_GetFirmwareVersion := GetProcAddress(dllhandle, 'DSP_GetFirmwareVersion');

    @DSP_UploadPicture := GetProcAddress(dllhandle, 'DSP_UploadPicture');

    @DSP_SetDefaultStyle := GetProcAddress(dllhandle, 'DSP_SetDefaultStyle');
    @DSP_SwitchStyle := GetProcAddress(dllhandle, 'DSP_SwitchStyle');
    @DSP_ShowPicture_3 := GetProcAddress(dllhandle, 'DSP_ShowPicture_3');
    @DSP_ShowQrCode_3 := GetProcAddress(dllhandle, 'DSP_ShowQrCode_3');
    @DSP_ShowText_3 := GetProcAddress(dllhandle, 'DSP_ShowText_3');

    @SetDebugFlag := GetProcAddress(dllhandle, 'SetDebugFlag');
  end;

  cbbPort.Items.Clear;
  if EnumComPorts(cbbPort.Items) then
    cbbPort.ItemIndex := 0;

  //style 3
  edt_amount.Text := '100.00';
  edt_filename_style3.Text := '';
  cbb_fileno_style3.Items.Clear;
  cbb_fileno_style3.Items.Clear;
  for i := Low(UploadFileInfo_PD280_STYLE3) to High(UploadFileInfo_PD280_STYLE3) do
    cbb_fileno_style3.Items.Add(UploadFileInfo_PD280_STYLE3[i].Caption);
  cbb_fileno_style3.ItemIndex := 0;

  ToggleEnabled;
{$IFDEF DEBUG}
  SetDebugFlag(True);
  Caption := Caption + '    --debug--';
{$ENDIF}
end;

procedure TfrmMain_PD280.ToggleEnabled;
begin
  btn_open.Enabled := not IsOpened;
  btn_close.Enabled := IsOpened;

  //display
  btn_restart.Enabled := IsOpened;
  btn_getfirmwareversion.Enabled := IsOpened;
  btn_init.Enabled := IsOpened;

  //style 3
  cbb_fileno_style3.Enabled := IsOpened;
  edt_filename_style3.Enabled := IsOpened;
  mmo_barcodetext_style3.Enabled := IsOpened;
  mmo_messagetext_style3.Enabled := IsOpened;
  edt_x_picture_style3.Enabled := IsOpened;
  edt_y_picture_style3.Enabled := IsOpened;
  edt_barcodesize.Enabled := IsOpened;
  edt_x_barcode_style3.Enabled := IsOpened;
  edt_y_barcode_style3.Enabled := IsOpened;
  edt_fontsize_style3.Enabled := IsOpened;
  edt_x_text_style3.Enabled := IsOpened;
  edt_y_text_style3.Enabled := IsOpened;
  btn_selectfile_style3.Enabled := IsOpened;
  btn_uploadpicture_style3.Enabled := IsOpened;
  btn_showpicture_style3.Enabled := IsOpened;
  btn_showbarcode_style3.Enabled := IsOpened;
  btn_showtext_style3.Enabled := IsOpened;
  //led8
  edt_amount.Enabled := IsOpened;
  btn_display_led8.Enabled := IsOpened;
  rg_lightstatus.Enabled := IsOpened;
end;

procedure TfrmMain_PD280.btn_openClick(Sender: TObject);
var
  iError: Integer;
begin
  if not OpenComm(AnsiString(cbbPort.Text), 115200) then
  begin
    ShowMessage('Error opening serial port');
    Exit;
  end;

  DSP_SetDefaultStyle(3);
  DSP_SwitchStyle(3);
  DSP_Init;

  IsOpened := True;
  ToggleEnabled;
end;

procedure TfrmMain_PD280.btn_RefreshClick(Sender: TObject);
begin
  if EnumComPorts(cbbPort.Items) then
    cbbPort.ItemIndex := 0;
end;

procedure TfrmMain_PD280.btn_restartClick(Sender: TObject);
begin
  DSP_Restart;
end;

procedure TfrmMain_PD280.btn_closeClick(Sender: TObject);
begin
  CloseComm;
  IsOpened := False;
  ToggleEnabled;
end;

procedure TfrmMain_PD280.btn_display_led8Click(Sender: TObject);
begin
  LED_Display(AnsiString(edt_amount.Text), rg_lightstatus.ItemIndex + 1);
end;

procedure TfrmMain_PD280.btn_selectfile_style3Click(Sender: TObject);
begin
  OpenPic1.InitialDir :=ExtractFilePath(Application.ExeName);
  if OpenPic1.Execute then
    edt_filename_style3.Text := OpenPic1.FileName;
end;

procedure TfrmMain_PD280.btn_showbarcode_style3Click(Sender: TObject);
var
  iError: Integer;
begin
  iError := DSP_ShowQrCode_3(UTF8Encode(mmo_barcodetext_style3.Text),
    StrToIntDef(edt_barcodesize.Text, 192),
    StrToIntDef(edt_x_barcode_style3.Text, 0),
    StrToIntDef(edt_y_barcode_style3.Text, 0));
//  ShowMessage(ExplainError(iError));
end;

procedure TfrmMain_PD280.btn_showpicture_style3Click(Sender: TObject);
var
  iError: Integer;
begin
  iError := DSP_ShowPicture_3(AnsiString(UploadFileInfo_PD280_STYLE3[cbb_fileno_style3.ItemIndex].Name),
    StrToIntDef(edt_x_picture_style3.Text, 0),
    StrToIntDef(edt_y_picture_style3.Text, 0));
//  ShowMessage(ExplainError(iError));
end;

procedure TfrmMain_PD280.btn_showtext_style3Click(Sender: TObject);
var
  iError: Integer;
begin
  iError := DSP_ShowText_3(UTF8Encode(mmo_messagetext_style3.Text),
    StrToIntDef(edt_fontsize_style3.Text, 32),
    StrToIntDef(edt_x_text_style3.Text, 0),
    StrToIntDef(edt_y_text_style3.Text, 0));
//  ShowMessage(ExplainError(iError));
end;

procedure TfrmMain_PD280.btn_uploadpicture_style3Click(Sender: TObject);
var
  iError: Integer;
begin
  if FileExists(edt_filename_style3.Text) then
  begin
    iError := DSP_UploadPicture(
      PAnsiChar(AnsiString(UploadFileInfo_PD280_STYLE3[cbb_fileno_style3.ItemIndex].Name)),
      PAnsiChar(AnsiString(edt_filename_style3.Text)));
    ShowMessage(ExplainError(iError));
  end;
end;

procedure TfrmMain_PD280.edt_amountKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in [#8, #13, #127, '.', '0'..'9']) then
    Key := #0;
end;

procedure TfrmMain_PD280.btn_exitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain_PD280.btn_getfirmwareversionClick(Sender: TObject);
var
  version: Integer;
begin
  version := DSP_GetFirmwareVersion;
  ShowMessage(IntToStr(version));
end;

procedure TfrmMain_PD280.btn_initClick(Sender: TObject);
begin
  DSP_Init;
end;

end.

